//
//  TCBarrgeContainer.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/16.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TCBarrgeContainer: NSView {
    var kWidth:CGFloat = 200
    
    var dataList:[ChatData] {
        return Td.sh().chats
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.wantsLayer = true
        self.layer?.backgroundColor = NSColor.clear.cgColor
        
        self.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    
    lazy var scrollView: NSScrollView = {
        let view = NSScrollView.init(frame: NSRect.zero)
        view.wantsLayer = true
        view.layer?.cornerRadius = 10
        view.layer?.masksToBounds = true
        view.alphaValue = 1.0
        view.backgroundColor = NSColor.clear
        view.documentView = self.tabView
        view.contentView.backgroundColor = NSColor.clear
        view.hasVerticalScroller = true
        view.autohidesScrollers = true
        view.automaticallyAdjustsContentInsets = false
        view.contentInsets = NSEdgeInsetsMake(0, 0, 0, 0)
        return view
    }()
    
    lazy var tabView: NSTableView = {
        let tab = NSTableView.init(frame: NSRect.zero)
        tab.wantsLayer = true
        tab.backgroundColor = NSColor.clear
        tab.delegate = self
        tab.dataSource = self
        tab.selectionHighlightStyle = .none
        
        let column = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier.init("column"))
        tab.addTableColumn(column)
        tab.headerView = nil
        return tab
    }()
}


extension TCBarrgeContainer :NSTableViewDelegate,NSTableViewDataSource{
    func numberOfRows(in tableView: NSTableView) -> Int {
        return self.dataList.count
    }
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        var cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier.init(mt_cell_default), owner: self) as? TCAudienceBarrgeCell
        
        if cell == nil {
            cell = TCAudienceBarrgeCell.init()
        }
        
        let info = self.dataList[row]
        cell?.nameLab.stringValue = info.formName
        cell?.textLab.stringValue = info.text
        cell?.isCurrent = Td.sh().name == info.formName
        return cell
    }
    
    func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        var scrollSpaceWidth:CGFloat = 0
        if #available(macOS 10.15, *) {
            scrollSpaceWidth = 40
        }
        let height = self.dataList[row].text.ga_height(font: 12, width: BarrgeContainerWidth-BarrgeContainerNameWidth-10-30-scrollSpaceWidth-2)
        return height+20
    }
}

